package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Demo7 {

	public static void main(String[] args) throws IOException {
		write("F:/pic/11.txt");
		readData("F:/pic/11.txt");
	}
	
	//读取顺序必须与写入一致，不一致数据会出现问题
	public static void readData(String srcPath) throws IOException{
		File file = new File(srcPath);
		DataInputStream is = new DataInputStream(
				new BufferedInputStream(
						new FileInputStream(file)));
		double d = is.readDouble();
		System.out.println(d);
		System.out.println(is.readLong());
		System.out.println(is.readUTF());
	}
	
	//数据类型处理流
	public static void write(String destPath) throws IOException{
		double p = 0.25d;
		long num = 100L;
		String str = "数据类型";
		
		File file = new File(destPath);
		DataOutputStream os = new DataOutputStream(
				new BufferedOutputStream(
						new FileOutputStream(file)));
		os.writeDouble(p);
		os.writeLong(num);
		//os.writeChars(str);
		os.writeUTF(str);
		os.flush();
		os.close();
	}
	
	//ByteArrayInputStream
	public static void read() throws IOException{
		String msg = "dsfhvhdfdzfv";
		byte[] data = msg.getBytes();
		
		InputStream bs = null;
		bs = new BufferedInputStream(new ByteArrayInputStream(data));
	    byte[] flush = new byte[1024];
	    int len = 0;
	    while(-1 != (len = bs.read(flush))){
	    	//
	    }
	    bs.close();
	}

}
